package com.yisoo.ubag.iot.manager;


import com.rabbitmq.client.Channel;
import com.yisoo.ubag.iot.entity.MsgBackEntity;
import com.yisoo.ubag.iot.entity.StatusEntity;
import com.yisoo.ubag.iot.feign.ProductFeignService;
import com.yisoo.ubag.iot.service.BoxRelBagService;
import com.yisoo.ubag.iot.service.DeviceService;
import com.yisoo.ubag.iot.service.MsgBackService;
import com.yisoo.ubag.iot.service.StatusService;
import com.yisoo.ubag.iot.vo.AckMqttContentVo;
import com.yisoo.ubag.iot.vo.DeviceStatusMqttContentVo;
import org.apache.tomcat.jni.Time;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;


@RabbitListener(queues = {"ubag.queue.pub"})
@Service("pubQueueManager")
public class PubQueueManager {

    @Autowired
    ProductFeignService productFeignService;
    @Autowired
    MsgBackService msgBackService;
    @Autowired
    StatusService statusService;
    @Autowired
    DeviceService deviceService;
    @Autowired
    BoxRelBagService boxRelBagService;


    @RabbitHandler
    public void recieveAckMqttContentVoMessage(Message message, AckMqttContentVo content, Channel channel) throws IOException {
        byte[] body = message.getBody();
        //消息头属性信息
        MessageProperties properties = message.getMessageProperties();
        //channel内按顺序自增的。
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        try {
            //业务处理
//            System.out.println(content);
            boxRelBagService.updateByAckMqttContentVo(content);
            //TODO: 得到绑定包后 发起停止汇报请求

            //收货
            channel.basicAck(deliveryTag,false);
        }catch (Exception e){
            //退回
            e.printStackTrace();
            channel.basicNack(deliveryTag,false,true);
        }
    }


//            case PUB_STATUS://发布消息
    @RabbitHandler
    public void recieveStatusEntityMessage(Message message, StatusEntity content, Channel channel) throws IOException {
        byte[] body = message.getBody();
        //消息头属性信息
        MessageProperties properties = message.getMessageProperties();
        //channel内按顺序自增的。
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        try {
            //业务处理
            System.out.println(content);
           statusService.saveOrUpdateByDeviceNameAndMsg(content);
            //收货
            channel.basicAck(deliveryTag,false);
        }catch (Exception e){
            //退回
            e.printStackTrace();
            channel.basicNack(deliveryTag,false,true);
        }
    }

//            case STATUS_CHANGE://状态改变
    @RabbitHandler
    public void recieveDeviceStatusMqttContentVoMessage(Message message, DeviceStatusMqttContentVo content, Channel channel) throws IOException {
        byte[] body = message.getBody();
        //消息头属性信息
        MessageProperties properties = message.getMessageProperties();
        //channel内按顺序自增的。
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        try {
            //业务处理
            //数据中的状态更新到各个数据库指定字段
            // 1
            if (content.getStatus().equals("online")){
                //1. pms_dev中的激活字段，保持为1即可
                //TODO 2.数据服务中的在线统计需要在这里写入数据库
                productFeignService.activeDev(content.getDeviceName(),1);
            }
            else if (content.getStatus().equals("offline")){
                productFeignService.activeDev(content.getDeviceName(),2);
            }else{
                System.out.println(">>>状态未知:"+content.getStatus());
            }
            // 2
            deviceService.updateOne(content);
            //收货
            channel.basicAck(deliveryTag,false);
        }catch (Exception e){
            //退回
            e.printStackTrace();//不要再删了，debug不到错误信息
            System.out.println("处理失败");
            channel.basicNack(deliveryTag,false,true);
        }
    }
    //直接备份
    @RabbitHandler
    public void recieveMsgBackEntityMessage(Message message, MsgBackEntity content, Channel channel) throws IOException {
        //{"id":1,"name":"哈哈","sort":null,"status":null,"createTime":1581144531744}
        System.out.println("MsgBackEntity原始消息备份："+content);
        byte[] body = message.getBody();
        //消息头属性信息
        MessageProperties properties = message.getMessageProperties();
        //channel内按顺序自增的。
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        try {
            //业务处理
            msgBackService.saveOrUpdate(content);
            //收货
            channel.basicAck(deliveryTag,false);
        }catch (Exception e){
            //退回
            e.printStackTrace();
            channel.basicNack(deliveryTag,false,true);
        }
    }

}
